/* 
 * File:   main.c
 * Author: frank
 *
 * Created on February 15, 2015, 12:24 PM
 */

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdbool.h>
#include <plib.h>   
#include "TickTimer.h"

#define SYS_FREQ	(40000000)
#pragma config FNOSC    = PRIPLL        // Oscillator Selection
#pragma config POSCMOD  = HS            // Primary Oscillator
#pragma config OSCIOFNC = ON            // CLKO Enable
#pragma config UPLLIDIV = DIV_2         // USB PLL Input Divider
#pragma config FPLLIDIV = DIV_2         // PLL Input Divider
#pragma config UPLLEN   = ON            // USB PLL Enabled
#pragma config FPLLMUL  = MUL_20        // PLL Multiplier
#pragma config FPLLODIV = DIV_2         // PLL Output Divider
#pragma config FPBDIV   = DIV_1         // Peripheral Clock divisor
#pragma config FWDTEN   = OFF           // Watchdog Timer
#pragma config WDTPS    = PS1           // Watchdog Timer Postscale
#pragma config FCKSM    = CSDCMD        // Clock Switching & Fail Safe Clock Monitor
#pragma config IESO     = OFF           // Internal/External Switch-over
#pragma config FSOSCEN  = OFF           // Secondary Oscillator Enable
#pragma config CP       = OFF           // Code Protect
#pragma config BWP      = OFF           // Boot Flash Write Protect
#pragma config PWP      = OFF           // Program Flash Write Protect
#pragma config ICESEL   = ICS_PGx1      // ICE/ICD Comm Channel Select
#pragma config DEBUG    = OFF           // Debugger Disabled for Starter Kit
#pragma config JTAGEN   = OFF           // No JTAG.  Need this for RB9 to work!


#define GetPeripheralClock()        (SYS_FREQ/(1<<OSCCONbits.PBDIV))

volatile uint8_t lastID;
volatile uint8_t count = 0;
static uint8_t test = 0;

void TickCallback1(uint8_t id){
    lastID = id;
    count++;
    mPORTAToggleBits(BIT_0);
}

int main(int argc, char** argv) {
    SYSTEMConfigPerformance(SYS_FREQ);
    TickTimer_Init();
    INTEnableSystemMultiVectoredInt();

    PORTSetPinsDigitalOut(IOPORT_A,BIT_0);
    PORTClearBits(IOPORT_A, BIT_0);

    uint8_t id1 = TickTimer_SetTickTime(true, 10);
    uint8_t id2 = TickTimer_SetTickTime(true, 100);
    uint8_t id3 = TickTimer_SetTickTime(false, 1000);

    TickTimer_SetTickCallbackID(&TickCallback1, id1);
    TickTimer_SetTickCallbackID(&TickCallback1, id2);
    TickTimer_SetTickCallbackID(&TickCallback1, id3);

    TickTimer_Start(id1);
    while(count < 1){
            test++;
    }

    TickTimer_Start(id2);
    while(count < 2){}

    TickTimer_Start(id3);
    while(1){}

    return (EXIT_SUCCESS);
}

